/*用于表单校验，需要jquery支持*/
var authentication = {
    //判断输入是否为空，如果为空加载提示信息
    isEmpty:function(txt){
        txt = $.trim(txt);
        if(!txt)
        {
            return true;
        }
        return false;
    },
    //校验用户名
    checkName:function(txt){
        if(this.isEmpty(txt)){
            return false;
        }
        //校验规则
        return (/\w{6,20}/.test(txt)) && (/[_a-zA-Z]+/.test(txt));
    },
    //是否整数
    isInt: function(txt){
        var pattern = /^\d*$/;
        return pattern.test(txt);
    },

    //是否浮点数
    isFloat: function(txt){
        var pattern = /^[1-9]\d*\.\d*$|0\.\d*[1-9]\d*$/;
        return pattern.test(txt);
    },

    //是否中文
    isChinese: function(txt){
        var pattern = /[\u4e00-\u9fa5]/;
        return pattern.test(txt);
    },
    //是否字母
    isLetter:function(txt){
        var patten = /^[A-Za-z]*$/;
        return patten.test(txt);
    },
    //校验密码
    //返回1：(正确)2：(密码１为空或格式不正确),3:(两次输入不一样)
    checkPassword:function(txt1,txt2){
        if(this.isEmpty(txt1)){
            return "2";
        }
        //var pattern = /[\x21-\x7E]{6,20}/;
        var pattern = /^(\w){6,20}$/;
        if( !pattern.test(txt1) ){
            return "2";
        }
        //重复密码
        if( txt1 != txt2 ){
            return "3";
        }
        return "1";
    },
    //校验mail
    checkMail:function(txt){
        if(!this.isEmpty(txt)){
            var pattern = /([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})/;
            //return pattern.test(txt);
            if(pattern.test(txt))
            {
                var newtxt = txt.split("@");
                if (newtxt[0].length>0 && newtxt[0].length<31)
                {
                    return true;
                }
            }
        }
        return false;
    },

    //格式化国际手机号码
    formatINTERMobile:function(txt){
        var mobileObj = {
            pre:'+',
            country_code:'',
            delimiter:'-',
            mobile:''
        };
        if(this.checkSimpleINTERMobile(txt)){
            var mobiles = new Array();
            mobiles = txt.split('-');
            var country_code;
            if(mobiles[0].indexOf('+')>-1){
                country_code = mobiles[0].substr(1);
            }else if(mobiles[0].substr(0,2) == '00'){
                country_code = mobiles[0].substr(2);
            }
            if(country_code !== 'undefined')
                mobileObj.country_code = country_code;

            mobileObj.mobile = mobiles[1];

        }
        return mobileObj;
    },
    //得到一个标准的手机号码
    getStdMobile:function(txt){

        if(this.checkSimpleCNMobile(txt)){
            return txt;
        }else {
            var mobileObj = this.formatINTERMobile(txt);
            if(mobileObj.country_code == '86')
                return mobileObj.mobile;
            else if(mobileObj.country_code !== '' && mobileObj.mobile !== '')
                return mobileObj.pre+mobileObj.country_code+mobileObj.delimiter+mobileObj.mobile;
        }
        return '';
    },

    //验证国内手机
    checkCNMobile:function(txt){
        //TODO 验证手机位数
        if(!this.isEmpty(txt)){
            if(this.checkSimpleCNMobile(txt)){
                return true;
            }
            var mobibesObj = this.formatINTERMobile(txt);
            if(mobibesObj.country_code == '86'){
                return true;
            }

        }
        return false;
    },

    //校验国际手机
    checkINTERMobile:function(txt){
        if(this.checkSimpleINTERMobile(txt)){
            var mobibesObj = this.formatINTERMobile(txt);
            if(mobibesObj.country_code !== '86'){
                return true;
            }
        }
        return false;
    },

    //简单校验国内手机形式
    checkSimpleCNMobile:function(txt){
        if(!this.isEmpty(txt)){
            var patten = /^1\d{10}$/;
            return patten.test(txt);
        }
        return false;
    },
    //简单校验国际手机形式
    checkSimpleINTERMobile:function(txt){
        if(!this.isEmpty(txt)){
            var patten = /(^([0]{2}|[+])\d{2,3}[-])\d{6,11}$/;
            return patten.test(txt);
        }
        return false;
    },
    //校验手机
    checkMobile:function(txt){
        if(!this.isEmpty(txt)){
            //var patten = /(^([0]{2}|[+])\d{2,3}[-])\d{6,11}$/;
            if(this.checkSimpleCNMobile(txt)){
                return true;
            }else if(this.checkSimpleINTERMobile(txt)){
                return true;
            }else{
                return false;
            }
        }
        return false;
    },
    //校验获取手机类型
    fetchMobileType:function(txt){

            if(this.checkSimpleCNMobile(txt)){
                return 'cn';
            }else if(this.checkSimpleINTERMobile(txt)){
                if(this.checkCNMobile(txt)){
                    return 'cn';
                }
                return 'inter';
            }else{
                return false;
            }

    },
    //真实姓名
    checkRealname:function(txt){
        txt = $.trim(txt);
        if(!this.isEmpty(txt)){
            if( /^[\u4e00-\u9fa5]+$/.test(txt) ){
                return /^[\u4e00-\u9fa5]{2,20}$/.test(txt);
            }
            else{
                return (/^[a-zA-Z]{2,}\s*\/\s*[a-zA-Z ]{2,}$/.test(txt)) || (/^[\u4e00-\u9fa5]+[A-z]{2,}$/.test(txt));
            }
        }
        return false;
    },
    //身份证检测，内部调用
    checkIdCard:function(txt){
        if(this.isEmpty(txt)){
            return false;
        }
        txt = txt.toUpperCase();
        var len = txt.length;

        //判断长度
        if((len != 15) && (len != 18)){
            return false;
        }

        //正则判断
        var pattern = /^(\d{6})(18|19|20)?(\d{2})([01]\d)([0123]\d)(\d{3})(\d|X|x)?$/i;
        if( !pattern.test(txt) ){
            return false;
        }

        //校验地区
        var city = {
            11:"北京",
            12:"天津",
            13:"河北",
            14:"山西",
            15:"内蒙古",
            21:"辽宁",
            22:"吉林",
            23:"黑龙江 ",
            31:"上海",
            32:"江苏",
            33:"浙江",
            34:"安徽",
            35:"福建",
            36:"江西",
            37:"山东",
            41:"河南",
            42:"湖北",
            43:"湖南",
            44:"广东",
            45:"广西",
            46:"海南",
            50:"重庆",
            51:"四川",
            52:"贵州",
            53:"云南",
            54:"西藏",
            61:"陕西",
            62:"甘肃",
            63:"青海",
            64:"宁夏",
            65:"新疆",
            71:"台湾",
            81:"香港",
            82:"澳门",
            91:"国外"
        };
        if( !city[parseInt(txt.substring(0,2),0)] ){
            return false;
        }

        //校验生日
        //如果15位补全4位年份
        txt = len == 15 ? (txt.substring(0,6) + '19' + txt.substring(6)) : txt;
        var year = txt.substring(6,10);
        var month = txt.substring(10,12);
        var day = txt.substring(12,14);
        var birth = new Date(year + '/' + month + '/' + day);
        //如果转换后的日期非法或者与填写的日期不符，退出
        if(!birth || birth.getFullYear() != year || birth.getMonth()+1 != month || birth.getDate() != day){
            return false;
        }

        //18位加权校验
        if(len == 18){
            var Wi = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2];
            var verify = [1,0,'X',9,8,7,6,5,4,3,2];
            var sum = 0;
            for(i=0; i<17; i++){
                sum += txt.charAt(i) * Wi[i];
            }
            var y = verify[sum % 11];
            if(y != txt.substring(17)){
                return false;
            }
        }
        return true;
    },
    //校验日期YYYYMMDD
    checkDate:function(txt){
      if(!this.isEmpty(txt))
        {
            var regex =/^(19|20)\d\d(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])$/;
            return regex.test(txt);
        }
        return false;
    },
    //校验日期YYY-MM-DD
    isDate:function (txt) {
        if(!this.isEmpty(txt))
        {
            var regex = new RegExp("^(?:(?:([0-9]{4}(-|\/)(?:(?:0?[1,3-9]|1[0-2])(-|\/)(?:29|30)|((?:0?[13578]|1[02])(-|\/)31)))|([0-9]{4}(-|\/)(?:0?[1-9]|1[0-2])(-|\/)(?:0?[1-9]|1\\d|2[0-8]))|(((?:(\\d\\d(?:0[48]|[2468][048]|[13579][26]))|(?:0[48]00|[2468][048]00|[13579][26]00))(-|\/)0?2(-|\/)29))))$");
            return regex.test(txt);
        }
        return false;
    },
    //校验生日
    isBrithday:function(txt)
    {

        if(this.isDate(txt))
        {
            var txtarr = txt.split("-");
            var d = new Date(txtarr[0],txtarr[1]*1-1,txtarr[2]);
            var n = new Date();
            if (d<n)
            {
                if (n.getFullYear()-d.getFullYear()<=100)
                {
                    return true;
                }
            }
        }
        return false;
    },
   
    //校验邮编
    checkPostal:function(txt){
        if(!this.isEmpty(txt)){
            var patten = /^\d{6}$/;
            return patten.test(txt);
        }
        return false;
    },
    //返回值：0：正确，１：超龄：２年龄不足
    dateDiffer:function(date_str,date_end,checktype)
    {
        re_meg = "0";
        data_str_arr = date_str.split("-");
        if ("CHILDREN" == checktype || "ADULT" == checktype)
        {
            add_num = 12; //12周岁
        }
        if ("BABY" == checktype)
        {
            add_num = 2; //2周岁
        }

        var daystr=new Date(data_str_arr[0]*1+add_num,data_str_arr[1]*1-1,data_str_arr[2]);
        if ("" ==date_end)
        {
            var dayend_str=new Date();
            var dayend=new Date(dayend_str.getFullYear(),dayend_str.getMonth(),dayend_str.getDate());
        }
        else
        {
            data_end_arr = date_end.split("-");
            var dayend=new Date(data_end_arr[0],data_end_arr[1]*1-1,data_end_arr[2]);
        }
        if("BABY" == checktype)
        {
            if (daystr <= dayend)//超龄
            {
                re_meg = "1";
            }
        }
        if("CHILDREN" == checktype)
        {

            baby_date = new Date(daystr.getFullYear()-10,daystr.getMonth(),daystr.getDate());
            if (daystr < dayend)//超龄
            {
                re_meg = "1";
            }
            else if(baby_date > dayend)//婴儿
            {
                re_meg = "2";
            }
        }
        if("ADULT" == checktype)
        {
            if (daystr > dayend)//超龄
            {
                re_meg = "2";
            }
        }
        return re_meg;
    },
    getBirthFromIdcard:function(txt)
    {
        var re_str = "";
        txt = txt.length == 15 ? (txt.substring(0,6) + '19' + txt.substring(6)) : txt;
        txt = txt.toUpperCase();
        var year = txt.substring(6,10);
        var	month = txt.substring(10,12);
        var	day = txt.substring(12,14);
        var birth = new Date(year + '/' + month + '/' + day);
        //如果转换后的日期非法或者与填写的日期不符，退出
        if(!(!birth || birth.getFullYear() != year || birth.getMonth()+1 != month || birth.getDate() != day)){
            re_str = year+"-"+month+"-"+day;
        }
        return re_str;
    }
};